﻿using System.Collections.Generic;
using System.Linq;
using AccelerateSensor.Database.SQLite;

namespace AccelerateSensor.Service.DbProxy.SQLite.Dal
{
    internal class Gateway
    {
        private static readonly string TableName = SqliteHelper.TableGateway;

        /// <summary>
        /// 添加网关
        /// </summary>
        /// <param name="model">网关对象</param>
        /// <returns>Rid</returns>
        public int Insert(Models.Gateway model)
        {
            if (model == null)
            {
                return 0;
            }

            var sql = $"insert into {TableName}(Uuid, Type, LocalUpdateTime, UpdateTime) " +
                      "values(@Uuid, @Type, @LocalUpdateTime, @UpdateTime);";
            return SqliteDbHelper.Insert(sql, model);
        }

        /// <summary>
        /// 批量添加网关
        /// </summary>
        /// <param name="models">网关对象集合</param>
        /// <returns>true成功，false失败</returns>
        public bool Insert(List<Models.Gateway> models)
        {
            if (models == null || models.Count == 0)
            {
                return false;
            }

            var sql = $"insert into {TableName}(Uuid, Type, LocalUpdateTime, UpdateTime) " +
                      "values(@Uuid, @Type, @LocalUpdateTime, @UpdateTime);";
            return SqliteDbHelper.Insert(sql, models.ToList<object>());
        }

        /// <summary>
        /// 更新网关(不修改网关Id)
        /// </summary>
        /// <param name="model">网关对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Update(Models.Gateway model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set Uuid = @Uuid, Type = @Type, " +
                      "    LocalUpdateTime = @LocalUpdateTime, UpdateTime = @UpdateTime " +
                      "where Rid = @Rid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 通过编号更新网关
        /// </summary>
        /// <param name="model">网关对象</param>
        /// <returns>true成功，false失败</returns>
        public bool UpdateByUuid(Models.Gateway model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set Type = @Type, LocalUpdateTime = @LocalUpdateTime, UpdateTime = @UpdateTime " +
                      "where Uuid = @Uuid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 删除网关
        /// </summary>
        /// <param name="model">网关对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Delete(Models.Gateway model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"delete from {TableName} where Rid = @Rid";
            return SqliteDbHelper.Delete(sql, model);
        }

        /// <summary>
        /// 查询网关
        /// </summary>
        /// <param name="rid">网关Id</param>
        /// <returns>网关对象</returns>
        public Models.Gateway GetModel(int rid)
        {
            var sql = $"select * from {TableName} where Rid = @Rid";
            var model = new Models.Gateway() { Rid = rid };
            return SqliteDbHelper.GetDataList<Models.Gateway>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询所有网关
        /// </summary>
        /// <returns>网关对象集合</returns>
        public List<Models.Gateway> GetAllModels()
        {
            var sql = $"select * from {TableName}";
            return SqliteDbHelper.GetDataList<Models.Gateway>(sql);
        }
    }
}
